package com.rd.student.jiaji.class3.wangyanbo.JulyTwelve;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Six {
    public static void main(String[] args) {
        Student Student1 = new Student("liusan",20,90);
        Student Student2 = new Student("lisi", 22, 90);
        Student Student3 = new Student("wangwu", 20, 99);
        Student Student4 = new Student("sunliu", 22, 100);
        ArrayList arrayList = new ArrayList();

        arrayList.add(Student1);
        arrayList.add(Student2);
        arrayList.add(Student3);
        arrayList.add(Student4);
        System.out.println(arrayList);


        Collections.sort(arrayList, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                Student d1 = (Student) o1;
                Student d2 = (Student) o2;

                if (d1.score < d2.score) {
                    return 1;

                }
                if (d1.score > d2.score) {
                    return -1;
                }
                if (d1.score==d2.score){
                    if (d1.age< d2.age) {
                        return 1;

                    }
                    if (d1.age > d2.age) {
                        return -1;
                    }
                }
                return 0;
            }
        });
        System.out.println(arrayList);
    }
}

class Student {
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
